package cn.dnui_cr503.dnuifood_16110100503.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.dnui_cr503.dnuifood_16110100503.R;
import cn.dnui_cr503.dnuifood_16110100503.bean.UserBean;
import cn.dnui_cr503.dnuifood_16110100503.listener.OnRetrofitListener;
import cn.dnui_cr503.dnuifood_16110100503.model.UserModel;


public class LoginActivity extends BaseActivity implements OnRetrofitListener<UserBean>{

    private Button btn_login,btn_reg;
    private EditText username,userpass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initViews();
        initEvents();
    }

    @Override
    void initViews() {
        layout_file=R.layout.activity_login;
        setLayout(layout_file);
        //初始化
        btn_login=(Button)findViewById(R.id.button_login);
        btn_reg = (Button)findViewById(R.id.button_reg);
        username =(EditText)findViewById(R.id.username);
        userpass =(EditText)findViewById(R.id.password);
    }

    @Override
    void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(username.getText().toString().trim().equals("")||userpass.getText().toString().trim().equals("")){
                    Toast.makeText(LoginActivity.this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    login();
                }
            }

        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    void initDate() {


    }

    @Override
    public void onSuccess(UserBean object, int flag) {
        //打开activity
        if(object.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }else {
          saveUser(object.getUserid());
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
        }
    }

    private void saveUser(String user_id) {
//        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor=sharedPreferences.edit();
//        editor.putString("username",username.getText().toString());
////        int userid=Integer.parseInt(user_id);
////       editor.putInt("user_id",userid);
//        editor.putString("user_id",user_id);
//        editor.putString("userpass",userpass.getText().toString());
//        editor.commit();

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编译器
        editor.putString("username",username.getText().toString());
        editor.putString("user_id",user_id);
//        int userid = Integer.parseInt(user_id);
//        editor.putInt("user_id",userid);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();

    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();
    }

    private void login(){
        UserModel userModel=new UserModel();
        userModel.login(username.getText().toString().trim(),userpass.getText().toString().trim(),this);
    }
}
